package com.tutorial.springboot.service.impl;

import com.tutorial.springboot.document.Post;
import com.tutorial.springboot.repository.PostRepository;
import com.tutorial.springboot.service.PostService;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.index.query.QueryStringQueryBuilder;
import org.elasticsearch.search.SearchHit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.ResultsExtractor;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.elasticsearch.core.query.SearchQuery;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by louchen on 2016/11/21.
 */
@Service
public class PostServiceImpl implements PostService {

    @Autowired
    private ElasticsearchTemplate elasticsearchTemplate;

    @Autowired
    private PostRepository postRepository;

    @Override
    public Post saveInEs(Post post) {
        Post afterSave = postRepository.save(post);
        return afterSave;
    }

    @Override
    public Post findByTitle(String title) {
        Post one = postRepository.findByTitle(title);
        return one;
    }

    @Override
    public List<Post> findByTitleLike(String title) {
        List<Post> list = postRepository.findByTitleLike(title);
        return list;
    }

    @Override
    public SearchResult findIdsByTitleCNLike(String titleCN, PageRequest pageRequest) {
        QueryStringQueryBuilder queryBuilder = new QueryStringQueryBuilder(titleCN);
        queryBuilder
                .analyzer("ik")
                .field("titleCN");
        SearchQuery searchQuery = new NativeSearchQueryBuilder()
                .withIndices("post")
                .withTypes("post")
                .withQuery(queryBuilder)
                .withPageable(pageRequest)
                .build();
        SearchResult result = elasticsearchTemplate.query(searchQuery, new ResultsExtractor<SearchResult>() {
            @Override
            public SearchResult extract(SearchResponse response) {
                long totalHits = response.getHits()
                        .totalHits();
                List<String> ids = new ArrayList<String>();
                for (SearchHit hit : response.getHits()) {
                    if (hit != null) {
                        ids.add(hit.getId());
                    }
                }
                return new SearchResult(ids, totalHits);
            }
        });
        return result;
    }

    public class SearchResult {

        List<String> ids;
        long count;

        public SearchResult(){}

        public SearchResult(List<String> ids,long count){
            this.ids=ids;
            this.count=count;
        }

        public List<String> getIds() {
            return ids;
        }

        public void setIds(List<String> ids) {
            this.ids = ids;
        }

        public long getCount() {
            return count;
        }

        public void setCount(long count) {
            this.count = count;
        }
    }

}
